//
//  HZUserAccount.swift
//  555wobo
//
//  Created by 黄某某 on 15/12/20.
//  Copyright © 2015年 黄某某. All rights reserved.
//

import UIKit

class HZUserAccount: NSObject,NSCoding{
    class var userLogin : Bool {
        return HZUserAccount.loadUserAccount() != nil
    }
    var access_token: String?
    var uid: String?
    var expires_in: NSTimeInterval = 0{
        didSet{
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var expires_Date: NSDate?
    var screen_name: String?
    var avatar_large: String?
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    override var description: String{
        get{
            return "access_token: \(access_token), uid: \(uid), expires_in: \(expires_in), expires_Date: \(expires_Date)"
        }
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    static let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.plist")
    func saveUserAccount(){
        NSKeyedArchiver.archiveRootObject(self, toFile: HZUserAccount.path)
    }
    func loadUserInfo(finished: (error: NSError?) -> ()){
        HZNetworking.sharedInstance.loadUserInfo { (result, error) -> () in
            if error != nil || result == nil{
                print("数据有误")
                finished(error: error)
            }
            self.screen_name = result!["screen_name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            self.saveUserAccount()
            HZUserAccount.userAccount=self
            finished(error: nil)
        }
    }
    static var userAccount : HZUserAccount?
    class func loadUserAccount() -> HZUserAccount?{
        if userAccount == nil{
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? HZUserAccount
        }
        if userAccount != nil && userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            print("加载到可用帐号")
            return userAccount
        }

//        if let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? HZUserAccount{
//            if userAccount.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
//                print("加载到可用账户")
//                return userAccount
//            }else{
//                print("帐号过期")
//                return nil
//            }
//        }else{
//         print("没加载到帐号")
//        }
        return nil
    }
}

//}error	NSError?	domain: "com.alamofire.error.serialization.response" - code: 18446744073709550605	0x00007f829bc38fa0
//[1]	(null)	"NSErrorFailingURLKey" : "https://api.weibo.com/2/users/show.json?access_token=2.002IqM1G0BGZvE05afbc1ab1jmBOvD&uid=5795384053"